package cn.zw.msmp.service.impl;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zw.msmp.dao.CustomerDao;
import cn.zw.msmp.pojo.CustomerInfo;
import cn.zw.msmp.service.ICustomerService;

@Service("customerService")
public class CustomerServiceImpl implements ICustomerService{

	private static final Logger log = LogManager.getLogger(CustomerServiceImpl.class);
	
	@Autowired
	private CustomerDao customerDao;
	
	@Override
	public void batchInsert(List<CustomerInfo> list) {
		try {
			customerDao.batchInsert(list);
		} catch (Exception e) {
			log.error("batch insert customer info error", e);
		}
		
	}

	@Override
	public CustomerInfo getCustomer(Integer id) {
		try {
			if(null != id) {
				return customerDao.getCustomer(id);
			}
		} catch (Exception e) {
			log.error("get customer by id error, id="+id, e);
		}
		return null;
	}

	@Override
	public void insert(CustomerInfo customerInfo) {
		try {
			customerDao.insert(customerInfo);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("insert customer info error", e);
		}
		
	}

	@Override
	public PageInfo<CustomerInfo> queryCustomers(CustomerInfo info, int pageIndex, int pageSize) {
		try {
			if(null != info) {
				PageHelper.startPage(pageIndex, pageSize);
				List<CustomerInfo> list = customerDao.queryCustomers(info);
				return new PageInfo<CustomerInfo>(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("get customer by id error, info="+info.toString(), e);
		}
		return null;
	}

	@Override
	public void delete(List<Integer> ids) {
		try {
			if(null != ids && ids.size() > 0) {
				customerDao.delete(ids);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("delete cusotmer info error", e);
		}
	}

	@Override
	public void update(CustomerInfo customerInfo) {
		try {
			if(null != customerInfo) {
				customerDao.update(customerInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("update customer error", e);
		}
	}
	
}
